//
//  HWMineInfoCell.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/8/22.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit

class HWMineInfoCell: UICollectionViewCell {
    
    var bgImageView:UIImageView!
    var headImageView:UIImageView!
    var headFrameImageView:UIImageView!
    var headButton:UIButton!
    var nameLabel:GranibleLabel!
    var openVipButton:UIButton!
//    var vipLevelView:UIView!
//    var vipLogoImageView:UIImageView!
//    var vipLevelLabel:UILabel!
    var signButton:UIButton!
    var signView:UIView!
    var signLogo:UIImageView!
    var signLabel:UILabel!
    var signNumLabel:UILabel!
//    var cardNumLabel:UILabel!
//    var cardLabel:UILabel!
//    var lineOne:UIView!
//    var cardButton:UIButton!
//    var scoreNumLabel:UILabel!
//    var scoreLabel:UILabel!
//    var lineTwo:UIView!
//    var scoreButton:UIButton!
//    var coinNumLabel:UILabel!
//    var coinLabel:UILabel!
//    var coinButton:UIButton!
//    var lineThree:UIView!
//    var appointmentNumLabel:UILabel!
//    var appointmentLabel:UILabel!
//    var appointmentButton:UIButton!
//    var collectionView:UICollectionView!

    var model:HWMineUserModel? {
        didSet {
            
            headImageView.kf.setImage(with: URL(string: model?.face ?? ""))
            headFrameImageView.sd_setImage(with: URL(string: model?.head_frame ?? ""), completed: nil)
//            if model?.is_huiyuan == false {
//                openVipButton.isHidden = false
//                vipLevelView.isHidden = true
//            } else {
//                openVipButton.isHidden = true
//                vipLevelView.isHidden = false
//                vipLogoImageView.kf.setImage(with: URL(string: model?.huiyuan_img ?? ""))
//                vipLevelLabel.text = "VIP\(model?.vip ?? "")"
//            }
//            
//            coinNumLabel.text = model?.coin
//            cardNumLabel.text = model?.coupon
//            scoreNumLabel.text = model?.point
//            appointmentNumLabel.text = model?.yuyue
            
            nameLabel.text = model?.user_name
            if (model?.color.count ?? 0) > 1 {
                var arr:[UIColor] = Array()
                for color in model?.color ?? [] {
                    arr.append(UIColor.colorWithHexString(hexString: color))
                }
                nameLabel.colors = arr
            }else {
                nameLabel.colors = [UIColor.colorWithHexString(hexString: model?.color[0] ?? "#FFFFFF"),UIColor.colorWithHexString(hexString: model?.color[0] ?? "#FFFFFF")]
            }
            
            if model?.is_qiandao == false {
                signButton.isHidden = false
                signView.isHidden = true
            } else {
                signButton.isHidden = true
                signView.isHidden = false
                signNumLabel.text = "已累计签到\(model?.qiandao_day ?? "")天"
            }
            
        }
    }
    
    var headButtonClick:(()->())?
    var signButtonClick:(()->())?
    var cardButtonClick:(()->())?
    var scoreButtonClick:(()->())?
    var coinButtonClick:(()->())?
    var appointmentButtonClick:(()->())?
    
    var clickItem:((_:NSInteger,_:HWMineUserModel)->())?
    
    var infoModel:HWMineModel? {
        didSet {
//            collectionView.reloadData()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWMineInfoCell {
    private func initUI() -> Void {
        bgImageView = UIImageView()
        bgImageView.image = UIImage(named: "mineTopBg")
        contentView.addSubview(bgImageView)
        
        headImageView = UIImageView()
        headImageView.image = UIImage(named: "AppIcon")
        headImageView.contentMode = .scaleAspectFill
        headImageView.layer.cornerRadius = 32*widthScale
        headImageView.layer.borderColor = UIColor.white.cgColor
        headImageView.layer.borderWidth = 1
        headImageView.layer.masksToBounds = true
        contentView.addSubview(headImageView)
        
        headFrameImageView = UIImageView()
        headFrameImageView.backgroundColor = .clear
        headFrameImageView.layer.masksToBounds = true
        headFrameImageView.contentMode = .scaleAspectFill
        contentView.addSubview(headFrameImageView)
        
        headButton = UIButton(type: .custom)
        headButton.addTarget(self, action: #selector(headButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(headButton)
        
        nameLabel = GranibleLabel()
//        nameLabel.text = "未登录"
        nameLabel.font = .boldSystemFont(ofSize: 20*widthScale)
//        nameLabel.colors = [UIColor.colorWithHexString(hexString: "#333333")]
        contentView.addSubview(nameLabel)
        
        openVipButton = UIButton(type: .custom)
        openVipButton.setTitle("VIP未开通", for: .normal)
        openVipButton.setTitleColor(.colorWithHexString(hexString: "#FF726A"), for: .normal)
        openVipButton.titleLabel?.font = .systemFont(ofSize: 9*widthScale, weight: .regular)
        openVipButton.layer.cornerRadius = 8*widthScale
        openVipButton.layer.borderWidth = 1*widthScale
        openVipButton.layer.borderColor = UIColor.colorWithHexString(hexString: "#FF726A").cgColor
        openVipButton.layer.masksToBounds = true
        contentView.addSubview(openVipButton)
        openVipButton.isHidden = true
        
//        vipLevelView = UIView()
//        vipLevelView.backgroundColor = .colorWithHexString(hexString: "#1CD67E")
//        vipLevelView.layer.cornerRadius = 8*widthScale
//        vipLevelView.layer.masksToBounds = true
//        contentView.addSubview(vipLevelView)
//        
//        vipLogoImageView = UIImageView()
//        vipLevelView.addSubview(vipLogoImageView)
//        
//        vipLevelLabel = HWLabel.createLabel(title: "", color: "#FFFFFF", font: 9*widthScale)
//        vipLevelView.addSubview(vipLevelLabel)
        
        signButton = UIButton(type: .custom)
        signButton.setImage(UIImage(named: "mineSignButton"), for: .normal)
        signButton.addTarget(self, action: #selector(signButtonClick(_:)), for: .touchUpInside)
        contentView.addSubview(signButton)
        
        signView = UIView()
        signView.backgroundColor = .white
        signView.frame = CGRect(x: 0, y: 0, width: 94*widthScale, height: 49*widthScale)
        contentView.addSubview(signView)
        signView.addCorner(conrners: [.topLeft,.bottomLeft], radius: 25*widthScale)
        
        signLogo = UIImageView()
        signLogo.image = UIImage(named: "signLogo")
        signView.addSubview(signLogo)
        
        signLabel = HWLabel.createBoldLabel(title: "已签到", color: "#F87706", font: 14*widthScale)
        signView.addSubview(signLabel)
        
        signNumLabel = HWLabel.createBoldLabel(title: "", color: "#F87706", font: 9*widthScale)
        signView.addSubview(signNumLabel)
        
//        cardNumLabel = HWLabel.createBoldLabel(title: "0", color: "#333333", font: 18*widthScale)
//        cardNumLabel.textAlignment = .center
//        contentView.addSubview(cardNumLabel)
//        
//        cardLabel = HWLabel.createLabel(title: "卡券", color: "#888888", font: 13*widthScale)
//        cardLabel.textAlignment = .center
//        contentView.addSubview(cardLabel)
//        
//        lineOne = UIView()
//        lineOne.backgroundColor = .colorWithHexString(hexString: "#CCCCCC")
//        contentView.addSubview(lineOne)
//        
//        scoreNumLabel = HWLabel.createBoldLabel(title: "0", color: "#333333", font: 18*widthScale)
//        scoreNumLabel.textAlignment = .center
//        contentView.addSubview(scoreNumLabel)
//        
//        scoreLabel = HWLabel.createLabel(title: "平台积分", color: "#888888", font: 13*widthScale)
//        scoreLabel.textAlignment = .center
//        contentView.addSubview(scoreLabel)
//        
//        lineTwo = UIView()
//        lineTwo.backgroundColor = .colorWithHexString(hexString: "#CCCCCC")
//        contentView.addSubview(lineTwo)
//        
//        coinNumLabel = HWLabel.createBoldLabel(title: "0", color: "#333333", font: 18*widthScale)
//        coinNumLabel.textAlignment = .center
//        contentView.addSubview(coinNumLabel)
//        
//        coinLabel = HWLabel.createLabel(title: "平台币", color: "#888888", font: 13*widthScale)
//        coinLabel.textAlignment = .center
//        contentView.addSubview(coinLabel)
//        
//        lineThree = UIView()
//        lineThree.backgroundColor = .colorWithHexString(hexString: "#CCCCCC")
//        contentView.addSubview(lineThree)
//        
//        appointmentNumLabel = HWLabel.createBoldLabel(title: "0", color: "#333333", font: 18*widthScale)
//        appointmentNumLabel.textAlignment = .center
//        contentView.addSubview(appointmentNumLabel)
//        
//        appointmentLabel = HWLabel.createLabel(title: "预约游戏", color: "#888888", font: 13*widthScale)
//        appointmentLabel.textAlignment = .center
//        contentView.addSubview(appointmentLabel)
//        
//        cardButton = UIButton(type: .custom)
//        cardButton.addTarget(self, action: #selector(cardButtonClick(_:)), for: .touchUpInside)
//        contentView.addSubview(cardButton)
//        
//        scoreButton = UIButton(type: .custom)
//        scoreButton.addTarget(self, action: #selector(scoreButtonClick(_:)), for: .touchUpInside)
//        contentView.addSubview(scoreButton)
//        
//        coinButton = UIButton(type: .custom)
//        coinButton.addTarget(self, action: #selector(coinButtonClick(_:)), for: .touchUpInside)
//        contentView.addSubview(coinButton)
//        
//        appointmentButton = UIButton(type: .custom)
//        appointmentButton.addTarget(self, action: #selector(appointmentButtonClick(_:)), for: .touchUpInside)
//        contentView.addSubview(appointmentButton)
 
//        let flowL = UICollectionViewFlowLayout.init()
//        flowL.scrollDirection = .horizontal
//        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: flowL)
//        collectionView.delegate = self
//        collectionView.dataSource = self
//        collectionView.backgroundColor = .clear
//        collectionView.translatesAutoresizingMaskIntoConstraints = false
//        collectionView.showsHorizontalScrollIndicator = false
//        collectionView.register(HWMineInfoVipCell.self, forCellWithReuseIdentifier: "HWMineInfoVipCell")
//        collectionView.register(HWMineSaveMoneyCardCell.self, forCellWithReuseIdentifier: "HWMineSaveMoneyCardCell")
//        contentView.addSubview(collectionView)
//        collectionView.contentInsetAdjustmentBehavior = .never
    }
    
    private func initFrame() -> Void {
        bgImageView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(331*widthScale)
        }
        
        headImageView.snp.makeConstraints { make in
            make.left.equalTo(18*widthScale)
            make.height.width.equalTo(64*widthScale)
            make.top.equalTo(92*widthScale)
        }
        
        headFrameImageView.snp.makeConstraints { make in
            make.center.equalTo(headImageView)
            make.width.height.equalTo(80*widthScale)
        }
        
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(headImageView.snp.right).offset(18*widthScale)
            make.top.equalTo(headImageView.snp.top).offset(7*widthScale)
            make.right.equalTo(signButton.snp.left)
            make.height.equalTo(27*widthScale)
        }
        
        openVipButton.snp.makeConstraints { make in
            make.left.equalTo(nameLabel)
            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
            make.height.equalTo(16*widthScale)
            make.width.equalTo(54*widthScale)
        }
        
//        vipLevelView.snp.makeConstraints { make in
//            make.left.equalTo(nameLabel)
//            make.top.equalTo(nameLabel.snp.bottom).offset(5*widthScale)
//            make.height.equalTo(16*widthScale)
//        }
//        
//        vipLogoImageView.snp.makeConstraints { make in
//            make.left.equalTo(5*widthScale)
//            make.width.height.equalTo(11*widthScale)
//            make.centerY.equalToSuperview()
//        }
//        
//        vipLevelLabel.snp.makeConstraints { make in
//            make.left.equalTo(vipLogoImageView.snp.right).offset(2*widthScale)
//            make.right.equalTo(-5*widthScale)
//            make.height.equalTo(13*widthScale)
//            make.centerY.equalToSuperview()
//        }
        
        signButton.snp.makeConstraints { make in
            make.right.equalTo(0)
            make.centerY.equalTo(headImageView)
            make.height.equalTo(33*widthScale)
            make.width.equalTo(94*widthScale)
        }
        
        signView.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.height.equalTo(50*widthScale)
            make.centerY.equalTo(headImageView)
        }
        
        signLogo.snp.makeConstraints { make in
            make.left.equalTo(14*widthScale)
            make.top.equalTo(10*widthScale)
            make.width.height.equalTo(16*widthScale)
        }
        
        signLabel.snp.makeConstraints { make in
            make.left.equalTo(signLogo.snp.right).offset(7*widthScale)
            make.centerY.equalTo(signLogo)
            make.right.equalTo(-13*widthScale)
        }
        
        signNumLabel.snp.makeConstraints { make in
            make.left.equalTo(signLogo)
            make.top.equalTo(signLogo.snp.bottom).offset(2*widthScale)
        }
        
//        cardNumLabel.snp.makeConstraints { make in
//            make.left.equalTo(0)
//            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
//            make.height.equalTo(25*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        cardLabel.snp.makeConstraints { make in
//            make.left.equalTo(0)
//            make.top.equalTo(cardNumLabel.snp.bottom).offset(5*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        cardButton.snp.makeConstraints { make in
//            make.left.equalTo(0)
//            make.top.equalTo(cardNumLabel)
//            make.bottom.equalTo(cardLabel)
//            make.width.equalTo(cardLabel)
//        }
//        
//        lineOne.snp.makeConstraints { make in
//            make.width.equalTo(1*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.left.equalTo(cardNumLabel.snp.right)
//            make.top.equalTo(cardNumLabel.snp.top).offset(17*widthScale)
//        }
//        
//        scoreNumLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineOne.snp.right)
//            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
//            make.height.equalTo(25*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        scoreLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineOne.snp.right)
//            make.top.equalTo(scoreNumLabel.snp.bottom).offset(5*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        scoreButton.snp.makeConstraints { make in
//            make.left.equalTo(lineOne.snp.right)
//            make.top.equalTo(scoreNumLabel)
//            make.bottom.equalTo(scoreLabel)
//            make.width.equalTo(scoreLabel)
//        }
//        
//        lineTwo.snp.makeConstraints { make in
//            make.width.equalTo(1*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.left.equalTo(scoreNumLabel.snp.right)
//            make.top.equalTo(scoreNumLabel.snp.top).offset(17*widthScale)
//        }
//        
//        coinNumLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineTwo.snp.right)
//            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
//            make.height.equalTo(25*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        coinLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineTwo.snp.right)
//            make.top.equalTo(coinNumLabel.snp.bottom).offset(5*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        coinButton.snp.makeConstraints { make in
//            make.left.equalTo(lineTwo.snp.right)
//            make.top.equalTo(coinNumLabel)
//            make.bottom.equalTo(coinLabel)
//            make.width.equalTo(coinLabel)
//        }
//        
//        lineThree.snp.makeConstraints { make in
//            make.width.equalTo(1*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.left.equalTo(coinNumLabel.snp.right)
//            make.top.equalTo(coinNumLabel.snp.top).offset(17*widthScale)
//        }
//        
//        appointmentNumLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineThree.snp.right)
//            make.top.equalTo(headImageView.snp.bottom).offset(18*widthScale)
//            make.height.equalTo(25*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        appointmentLabel.snp.makeConstraints { make in
//            make.left.equalTo(lineThree.snp.right)
//            make.top.equalTo(appointmentNumLabel.snp.bottom).offset(5*widthScale)
//            make.height.equalTo(18*widthScale)
//            make.width.equalTo((screenWidth-3*widthScale)/4)
//        }
//        
//        appointmentButton.snp.makeConstraints { make in
//            make.left.equalTo(lineThree.snp.right)
//            make.top.equalTo(appointmentNumLabel)
//            make.bottom.equalTo(appointmentLabel)
//            make.width.equalTo(appointmentLabel)
//        }
//        
//        collectionView.snp.makeConstraints { make in
//            make.left.right.bottom.equalToSuperview()
//            make.top.equalTo(cardLabel.snp.bottom).offset(18*widthScale)
//        }
        
        headButton.snp.makeConstraints { make in
            make.left.right.top.bottom.equalTo(headImageView)
        }
        
    }
}

extension HWMineInfoCell {
    @objc func cardButtonClick(_:UIButton) -> Void {
        self.cardButtonClick?()
    }
    
    @objc func scoreButtonClick(_:UIButton) -> Void {
        self.scoreButtonClick?()
    }
    
    @objc func coinButtonClick(_:UIButton) -> Void {
        self.coinButtonClick?()
    }
    
    @objc func appointmentButtonClick(_:UIButton) -> Void {
        self.appointmentButtonClick?()
    }
    
    @objc func headButtonClick(_:UIButton) -> Void {
        self.headButtonClick?()
    }
    
    @objc func signButtonClick(_:UIButton) -> Void {
        self.signButtonClick?()
    }
}

//class HWMineInfoCellCollectionProtocol: NSObject {
//
//}

extension HWMineInfoCell: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.clickItem?(indexPath.row,model!)
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 7*widthScale
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: -5*widthScale, left: 18*widthScale, bottom: 0, right: 0)
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 225*widthScale, height: 91*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return  2
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let saveMoneyCardCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineSaveMoneyCardCell", for: indexPath) as! HWMineSaveMoneyCardCell
        let vipCell = collectionView.dequeueReusableCell(withReuseIdentifier: "HWMineInfoVipCell", for: indexPath) as! HWMineInfoVipCell
        if indexPath.row == 0 {
            saveMoneyCardCell.model = infoModel?.shengqianka
            return saveMoneyCardCell
        } else if indexPath.row == 1 {
            vipCell.model = infoModel?.huiyuan
        }
        return vipCell
        
        
    }
    
}
